import {Component, OnInit} from '@angular/core';
import {CommonPage} from '../common-page';
import {RoleComponent} from '../role/role.component';
import {MenuComponent} from '../menu/menu.component';
import {ProjectUserComponent} from '../projectuser/project.user.component';
import {ObjectUtil} from '../../../shared/utils/object-util';
import {CacheKey, SelectType} from '../../../shared/components/dhlcomponent/enums';
import {ProjectFormComponent} from './form/project-form.component';

@Component({
  templateUrl: './project.component.html'
})
export class ProjectComponent extends CommonPage implements OnInit {

  datas = [];
  tabDatas = [];
  projectId;
  showEdit = true;
  showAdd = true;
  showDelete = true;

  ngOnInit() {
    this.requestService.get('project/list').subscribe((data) => {
      const loginUser = this.cacheService.getItem(CacheKey.LOGIN_USER);
      const projects = this.cacheService.getItem(CacheKey.MANAGE_PROJECTS);
      if (data) {
        if (loginUser.isSuperManager) {
          this.datas = data.map(p => ({...p, title: p.enName, content: p.name}));
        } else if (ObjectUtil.isArray(projects) && projects.length > 0) {
          const projectCodes = projects.map(p => p.code).reduce((x, y) => x + ',' + y);
          this.datas = data
            .filter(({code}) => projectCodes.indexOf(code) > -1)
            .map(p => ({
              ...p,
              title: p.enName,
              content: p.name
            }));
          this.showEdit = false;
          this.showDelete = false;
          this.showAdd = false;
        }

        // if (projects.length == 0) {
        //   this.showAdd = false;
        // }

      } else if (!loginUser.isSuperManager) {
        this.showEdit = false;
        this.showAdd = false;
        this.showDelete = false;
      }

    });
  }


  cardClick({data}) {
    let tabs = [];

    if (data) {

      const loginUser = this.cacheService.getItem(CacheKey.LOGIN_USER);
      const projects = this.cacheService.getItem(CacheKey.MANAGE_PROJECTS);

      let isManager = false;
      for (const p of projects) {
        if (p.code == data.code) {
          isManager = p.manager;
        }
      }

      const project = {
        projectId: data.id,
        projectCode: data.code,
        showDataPer: data.code && data.code == 'rpmp' ? true : false,
        selectType: data.code && data.code == 'rpmp' ? SelectType.DEFAULT : SelectType.MULTIPLE,
        menuRootCode: data.code + '_menu_root'
      };

      tabs = [{
        title: '用戶',
        data: project,
        component: ProjectUserComponent
      }];

      if (isManager || loginUser.isSuperManager) {
        tabs.push({
          title: '角色',
          data: project,
          component: RoleComponent
        });
      }

      if (loginUser.isSuperManager) {
        tabs.push({
          title: '菜单权限',
          data: project,
          component: MenuComponent
        });
      }

    }


    this.tabDatas = tabs;
  }

  editClick({index, data}) {
    this.windowService.openWindow({
      submitUrl: 'project/save',
      data,
      width: 800,
      component: ProjectFormComponent,
      title: '新增项目',
      afterOk: ({formData}) => {
        this.datas.splice(index, 1, {...formData, title: formData.enName, content: formData.name});
      }
    });
  }


  deleteClick({finishedDelete, data: {id}}) {
    this.requestService.post(`project/delete/${id}`).subscribe((data) => {
      if (data) {
        finishedDelete();
      }
    });
  }

  addCard({finishedAdd}) {
    this.windowService.openWindow({
      submitUrl: 'project/save',
      width: 600,
      component: ProjectFormComponent,
      title: '新增项目',
      afterOk: ({formData: obj}) => {
        const {name, code, id, enName} = obj;
        this.datas.push({...obj, id, title: enName, content: name, code});
        finishedAdd();
      }
    });
  }

}




